package com.lan.ot;

import com.lan.model.Type;
import com.lan.model.Variable;
import com.lan.model.symbol.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClazzOt implements Ot {


    /**
     * data
     * :LITERAL
     */
    private Json json;

    /**
     * value
     * :LITERAL
     * :Function
     * :Function
     * :数组
     */
    private Map<Variable, Object> variables = new HashMap<>();


    private Map<String, Variable> keyVariable = new HashMap<>();

//    private ClazzOt parentOt;

//    private Map<String, FunctionOt> functions = new HashMap<>();

    /**
     * 数据
     */
    private Object data;


    public ClazzOt(Json Json) {
        this.json = Json;
        this.data = json.getCopyData();
    }

    public ClazzOt(Object data) {
        this.data = data;
    }

    public void setJson(Json Json) {
        this.json = Json;
        this.data = json.getCopyData();
    }

    @Override
    public Type type() {
        return Type.UC;
    }

    @Override
    public Object getValue(Variable variable) {
        return variables.get(variable);
    }

    @Override
    public Variable getVariable(String name) {
        Variable variable = keyVariable.get(name);
        if (variable == null) {
            if (json != null) {
                variable = json.getVariable(name);
                if (variable != null) {
                    keyVariable.put(variable.getName(), variable);
                    variables.put(variable, variable.getValue());
                    // 添加引用
                    if (variable.getValue() instanceof ClazzOt) {
                        ClazzOt oot = (ClazzOt) variable.getValue();
                        if (data instanceof Map) {
                            Map<String, Object> map = (Map<String, Object>) data;
                            map.put(variable.getName(), oot.getData());
                        } else if (data instanceof List) {
                            List<Object> list = (List<Object>) data;
                            list.set(Integer.valueOf(name), oot.getData());
                        }
                    }
                }
            }

            if (variable == null) {
                // data 不是 key
                // 暂时可以不使用  设置时  不需要获取数据
                if (data instanceof Map) {
                    Map<String, Object> map = (Map<String, Object>) data;
                    Object v = map.get(name);
                    if (v != null) {
                        variable = new Variable(Type.Integer, name, v);
                    }
                } else if (data instanceof List) {
                    List<Object> list = (List<Object>) data;
                    Object v = list.get(Integer.valueOf(name));
                    if (v != null) {
                        variable = new Variable(Type.Integer, name, v);
                    }
                }
            }
        }
        return variable;
    }

    @Override
    public void setValue(Variable variable, Object value) {
        keyVariable.put(variable.getName(), variable);
        variables.put(variable, value);

        // []  {}  兼容
        if (variable.getType() != null && value != null) {
            if (data instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) data;
                if (value instanceof ClazzOt) {
                    ClazzOt oot = (ClazzOt) value;
                    map.put(variable.getName(), oot.getData());
                } else {
                    map.put(variable.getName(), value);
                }
            } else {
                List<Object> list = (List<Object>) data;
                if (value instanceof ClazzOt) {
                    ClazzOt oot = (ClazzOt) value;
                    list.set(Integer.valueOf(variable.getName()), oot.getData());
                } else {
                    list.set(Integer.valueOf(variable.getName()), value);
                }
            }
//            if (variable.getType() == Type.Expression_KEY || variable.getType() == Type.ClazzOt) {
//                Map<String, Object> map = (Map<String, Object>) data;
//                if (value instanceof ClazzOt) {
//                    ClazzOt oot = (ClazzOt) value;
//                    map.put(variable.getName(), oot.getData());
//                } else {
//                    map.put(variable.getName(), value);
//                }
//            } else if (variable.getType() == Type.Expression_Array) {
//                List<Object> list = (List<Object>) data;
//                list.set(Integer.valueOf(variable.getName()), value);
//            }
        }
    }


    public Json getJson() {
        return json;
    }


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Map<String, Variable> getKeyVariable() {
        return keyVariable;
    }

//    public ClazzOt getParentOt() {
//        return parentOt;
//    }
//
//    public void setParentOt(ClazzOt parentOt) {
//        this.parentOt = parentOt;
//    }
}
